//
//  AIGirlHomeTopView.swift
//  AIGirl
//
//  Created by Rae on 2023/12/28.
//

import UIKit

class AIGirlHomeTopView: UICollectionReusableView {
    
    private var premiumView = AIGirlHomePremiumView()
    private var alreadyPreView = AIGirlHomeAlreadyPremiumView()
    private var bestView = AIGirlHomeBestDealView()
    
    var model: AIGirlHomeItemModel?{
        didSet{
            guard let md = model else { return }
            bestView.model = md
            updateView()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            premiumView
            alreadyPreView
            bestView
        }
        
        premiumView.leading(0).top(0).trailing(0).height(225)
        alreadyPreView.leading(0).top(0).trailing(0).height(120)
        bestView.leading(0).trailing(0).height(175).bottom(25)
//        bestView.Top == premiumView.Bottom
        
        
        updateView()
        NotificationCenter.default.addObserver(forName: .updateVIPState, object: nil, queue: nil) { noti in
            self.updateView()
        }
    }
    
    private func updateView(){

        if GlobalTool.userModel?.isVip.intValue ?? 0 > 0 {
            premiumView.isHidden = true
            alreadyPreView.isHidden = false
            alreadyPreView.updateTime()
        }else{
            premiumView.isHidden = false
            alreadyPreView.isHidden = true
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

}


class AIGirlHomePremiumView: UIView {

    private lazy var bgView = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_premiumBg").mode(.scaleAspectFill).end()
    private lazy var diamondImageView = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_preDiamond").mode(.scaleAspectFill).end()
    private lazy var titleImageView = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_premium").mode(.scaleAspectFill).end()
    private lazy var checkImageView_one = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_check").mode(.scaleAspectFill).end()
    private lazy var checkImageView_two = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_check").mode(.scaleAspectFill).end()
    private lazy var checkImageView_three = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_check").mode(.scaleAspectFill).end()
    private lazy var checkTitle_one = c_make_ex(UILabel.self)
        .text("Unlock Intimate Conversations.").color("#FBD97D")
        .font(15.font_medium).end()
    private lazy var checkTitle_two = c_make_ex(UILabel.self)
        .text("Exclusive VIP Avatar.").color("#FBD97D")
        .font(15.font_medium).end()
    private lazy var checkTitle_three = c_make_ex(UILabel.self)
        .text("Log in every day to get free messages").color("#FBD97D")
        .font(15.font_medium).end()
    private lazy var joinButton = c_make_ex(UIButton.self)
        .bgImage("AIGirl_Home_premiumBtn").corner(20)
        .text("Join Now").color("#643208").font(21.font_bold).end()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            bgView.subviews{
                diamondImageView
                titleImageView
                checkImageView_one
                checkImageView_two
                checkImageView_three
                checkTitle_one
                checkTitle_two
                checkTitle_three
                joinButton
            }
        }
        
        bgView.leading(10).trailing(10).top(20).height(203)
        diamondImageView.leading(15).top(17).width(29).height(26)
        titleImageView.leading(45.5).top(20.5).width(109).height(18)
        checkImageView_one.leading(18).width(11).height(9)
        checkImageView_one.Top == diamondImageView.Bottom + 20.5
        checkImageView_two.leading(18).width(11).height(9)
        checkImageView_two.Top == checkImageView_one.Bottom + 17.5
        checkImageView_three.leading(18).width(11).height(9)
        checkImageView_three.Top == checkImageView_two.Bottom + 17.5
        checkTitle_one.leading(38)
        checkTitle_one.CenterY == checkImageView_one.CenterY
        checkTitle_two.leading(38)
        checkTitle_two.CenterY == checkImageView_two.CenterY
        checkTitle_three.leading(38)
        checkTitle_three.CenterY == checkImageView_three.CenterY
        joinButton.width(295).height(40).centerHorizontally()
        joinButton.Top == checkImageView_three.Bottom + 19.5
        bgView.isUserInteractionEnabled = true
        joinButton.addTarget(self, action: #selector(clickJoinButton), for: .touchUpInside)
    }
    
    @objc private func clickJoinButton(){
        GlobalTool.showVipInfoPage()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class AIGirlHomeAlreadyPremiumView: UIView {

    private lazy var bgView = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_alreadyPremiumBg").mode(.scaleAspectFill).end()
    private lazy var titleImageView = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_alreadyTitleView").mode(.scaleAspectFill).end()
    private lazy var checkImageView = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_check").mode(.scaleAspectFill).end()
    private lazy var checkTitle = c_make_ex(UILabel.self)
        .text("Unlock Intimate Conversations.").color("#FBD97D")
        .font(15.font_medium).end()
    private lazy var timeBgView = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_alreadyTimeBg").end()
    private lazy var timeLabel = c_make_ex(UILabel.self)
        .color("#FFEA79")
        .font(15.font_semiItalic).end()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            bgView.subviews{
                titleImageView
                checkImageView
                checkTitle
                timeBgView
                timeLabel
            }
        }
        
        bgView.leading(10).trailing(10).top(20).height(100)
        titleImageView.leading(15).top(17).width(140).height(26)
        checkImageView.leading(18).top(64).width(11).height(9)
        checkTitle.leading(38)
        checkTitle.CenterY == checkImageView.CenterY
        timeBgView.trailing(5).width(185).height(25)
        timeBgView.CenterY == titleImageView.CenterY
        timeLabel.CenterY == timeBgView.CenterY
        timeLabel.CenterX == timeBgView.CenterX
        
        updateTime()
        
        let tapG = UITapGestureRecognizer(target: self, action: #selector(pushToAlreadyPreVC))
        bgView.isUserInteractionEnabled = true
        bgView.addGestureRecognizer(tapG)
    }
    
    func updateTime(){
        
        let startT = UserDefaultsManager.getString(forKey: .VIPStartTime) ?? ""
        let endT = UserDefaultsManager.getString(forKey: .VIPEndTime) ?? ""
        if startT.count > 0 && endT.count > 0 {
            timeLabel.text = startT + "-" + endT
        }
    }
    
    @objc private func pushToAlreadyPreVC(){
        GlobalTool.showAlreadyVipInfoPage()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class AIGirlHomeBestDealView: UIView {

    private lazy var bgView = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_bestBg").mode(.scaleAspectFill).end()
    private lazy var bestImageView = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_bestDeal").mode(.scaleAspectFill).end()
    private lazy var diamondImageView = c_make_ex(UIImageView.self)
        .image("AIGirl_Home_purpleDiamond").mode(.scaleAspectFill).end()
    private lazy var diamondLabel = c_make_ex(UILabel.self)
        .text("20").color(.white).font(20.font_bold).end()
    private lazy var joinButton = c_make_ex(UIButton.self)
        .bgImage("AIGirl_Home_bestButton")
        .text("$ 49.99").color("#643208").font(16.font_bold).end()
    
    var model: AIGirlHomeItemModel?{
        didSet{
            guard let md = model else { return }
            diamondLabel.text = md.coin
            joinButton.setTitle("$ " + md.price, for: .normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            bgView.subviews{
                bestImageView
                diamondImageView
                diamondLabel
                joinButton
            }
        }
        
        bgView.leading(10).trailing(10).top(27).height(148)
        bestImageView.leading(0).top(19).width(148).height(33)
        diamondImageView.leading(48).top(66).size(30)
        diamondLabel.Leading == diamondImageView.Trailing + 5
        diamondLabel.CenterY == diamondImageView.CenterY
        joinButton.leading(27).width(110).height(33)
        joinButton.Top == diamondImageView.Bottom + 10
        joinButton.isUserInteractionEnabled = false
        
        let tapG = UITapGestureRecognizer(target: self, action: #selector(buyCoin))
        bgView.isUserInteractionEnabled = true
        bgView.addGestureRecognizer(tapG)
    }
    
    @objc private func buyCoin(){
        
        guard let md = model else { return }
        purchaseTool.purchaseProduct(productCode: md.code, productId: md.id)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
